<?php
	///////////////////////////////////////////////////////////////////////////////////////////////////
	//	PAY PERIOD FUNCTIONS
	//	WRITTEN BY: TRAVIS THORNTON
	//	12-07-2009
	//
	//	What this page does:
	//	1) Determines if a new payperiod row needs to be inserted into the db
	//	2) Sets 2 globals relating to the current and last time period
	//
	////////////////////////////////////////////////////////////////////////////////////////////////////
	
	require_once 'php_includes.php';
	require_once "admin_options.php";
		
	//Global for period start
	$global_current_period_start = 0;
	$global_last_period_start = 0;

	//Checks to see if we need to add a row in the table for the current pay period
	function checkDBPeriod($user)
	{
		//use global variables
		global $admin_initial_period_start;
		global $global_current_period_start;
		global $global_last_period_start;
		global $dbname;
		
		//var to track the date we are looking for
		$tracking_date = strtotime($admin_initial_period_start);
		
		//The current time
		$current_date = time();
		
		//variables used to store current week and previous week since the result of the while loop will be the next week
		$previous_week = 0;
		$current_week = 0;
		
		//loop until wew find a period start date after the current time
		while($tracking_date < $current_date)
		{
			//save previous week
			$previous_week = $current_week;
			
			//save current week
			$current_week = $tracking_date;
			//save current week end of period date
			$end_period = strtotime(date("m/d/y", $tracking_date) . " + 13 days");
			
			//move the time tracker 2 weeks ahead
			$this_period = date("m/d/y", $tracking_date);
			$tracking_date = strtotime($this_period . " + 2 weeks");//using +2 weeks due to a problem I found with nov 1st repeating when just adding seconds
		}
		
		//echo "Next Period: " . date("Y-m-d", $tracking_date) ."<br/>\n" . "This Period: " . date("Y-m-d", $current_week) . " -> " . date("Y-m-d", $end_period). "<br/>\n Previous Period: " . date("Y-m-d", $previous_week);
		$global_current_period_start = date("Y-m-d", $current_week);
		$global_last_period_start = date("Y-m-d", $previous_week);
		
		//see if there is a row in the db
		sqlConnect();
	
		$sql = "SELECT EmployeeID FROM PayPeriod WHERE EmployeeID = '". $user ."' AND PeriodStart LIKE('". $global_current_period_start ."')";
		$results = sqlQuery($sql);
	
		if(!mysql_result($results, 0, 0))
		{
			//Update the previous time period to the "submitted" status automatically.
			$sql = 'UPDATE `' . $dbname . '`.`PayPeriod` SET PeriodStatus = \'1\' WHERE EmployeeID = \''. $user .'\' AND PeriodStatus = \'0\'';		
			sqlQuery($sql);
			
			//insert a row into the table for the period
			$sql = 'INSERT INTO `' . $dbname . '`.`PayPeriod` (`EmployeeID`, `PeriodStart`, `PeriodEnd`, `PeriodStatus`, `Entry1`, `Entry2`, `Entry3`, `Entry4`, `Entry5`, `Entry6`, `Entry7`, `Entry8`, `Entry9`, `Entry10`, `Entry11`, `Entry12`, `Entry13`, `Entry14`) VALUES (\'' . $user . '\', \'' . $global_current_period_start . '\', \'' . date("Y-m-d", $end_period) . '\', \'0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\', \'4@0\');';
			sqlQuery($sql);				
		}

		sqlExit();	
	}
?>